package com.example.opengl.camera.mediarecord

import android.hardware.Camera
import android.media.CamcorderProfile
import android.media.MediaRecorder
import android.os.Build
import android.util.Log
import android.view.Surface
import androidx.annotation.RequiresApi
import com.android.baselibrary.app.MainApplication
import com.example.opengl.camera.inter.IRecordVideoCallback
import java.io.File

/**
 * @author: Mr.You
 * @create: 2023-10-30 13:12
 * @description:使用MediaRecord录制视频
 **/

class MediaRecordModel : IRecordVideoCallback {
    private var mMediaRecord: MediaRecorder = MediaRecorder()

    init {

        mMediaRecord.setAudioSource(MediaRecorder.AudioSource.MIC)
        mMediaRecord.setVideoSource(MediaRecorder.VideoSource.CAMERA)
        mMediaRecord.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
        mMediaRecord.setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
        mMediaRecord.setVideoEncoder(MediaRecorder.VideoEncoder.H264)
        //mMediaRecord.setOrientationHint(90)
        val camcorderProfile = CamcorderProfile.get(0, CamcorderProfile.QUALITY_1080P)
        Log.i("TAG_MediaRecord", ": width:${camcorderProfile.videoFrameWidth} , height:${camcorderProfile.videoFrameHeight}")

        // mMediaRecord.setVideoSize(camcorderProfile.videoFrameHeight,camcorderProfile.videoFrameWidth)
    }
    fun setCamera(camera: Camera){
        camera.unlock()
        mMediaRecord.setCamera(camera)

    }


    @RequiresApi(Build.VERSION_CODES.M)
    override fun setSurface(surface: Surface) {

         // mMediaRecord.setInputSurface(surface)

          mMediaRecord.setPreviewDisplay(surface)
    }


    override fun startRecord() {
        mMediaRecord.prepare()
        mMediaRecord.start()
    }

    override fun setVideoSize(width: Int, height: Int) {

    }

    @RequiresApi(Build.VERSION_CODES.N)
    override fun stopRecord() {
        mMediaRecord.pause()
    }

    @RequiresApi(Build.VERSION_CODES.O)
    override fun setOutFilePath(path: String) {

        mMediaRecord.setOutputFile(File(path))
    }

    override fun release() {
        mMediaRecord.stop()
        mMediaRecord.release()
    }
}